package com.hyh.elasticsearch.helper;

import org.elasticsearch.action.bulk.BulkRequest;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteRequest;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.action.index.IndexResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.RequestOptions;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.client.indices.CreateIndexRequest;
import org.elasticsearch.client.indices.CreateIndexResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.Assert;

import java.io.IOException;
import java.util.List;

/**
 * Elasticsearcj 助手
 *
 * @Author: heyuhua
 * @Date: 2021/2/4 11:52
 */
@Component
public class ElasticsearchHelper {

    @Autowired
    private RestHighLevelClient restHighLevelClient;

    private final RequestOptions DEFAULT_REQUES_OPTION = RequestOptions.DEFAULT;

    /**
     * 创建索引
     *
     * @param index 索引名称
     * @return
     * @throws IOException
     */
    public CreateIndexResponse createIndex(String index) throws IOException {
        Assert.notNull(index, "index is null.");
        CreateIndexRequest createIndexRequest = new CreateIndexRequest(index);
        return createIndex(createIndexRequest);
    }

    /**
     * 创建索引
     *
     * @param createIndexRequest 创建索引Request对象
     * @return
     * @throws IOException
     */
    public CreateIndexResponse createIndex(CreateIndexRequest createIndexRequest) throws IOException {
        Assert.notNull(createIndexRequest, "index object is null.");
        return restHighLevelClient.indices().create(createIndexRequest, DEFAULT_REQUES_OPTION);
    }


    /**
     * 创建索引
     *
     * @param indexRequest
     * @return
     * @throws IOException
     */
    public IndexResponse createIndex(IndexRequest indexRequest) throws IOException {
        Assert.notNull(indexRequest, "index request object is null.");
        return restHighLevelClient.index(indexRequest, DEFAULT_REQUES_OPTION);
    }


    /**
     * 更新索引
     *
     * @param updateRequest
     * @return
     * @throws IOException
     */
    public UpdateResponse updateIndex(UpdateRequest updateRequest) throws IOException {
        Assert.notNull(updateRequest, "update request object is null.");
        return restHighLevelClient.update(updateRequest, DEFAULT_REQUES_OPTION);
    }


    /**
     * 删除索引
     *
     * @param deleteRequest
     * @return
     * @throws IOException
     */
    public DeleteResponse deleteIndex(DeleteRequest deleteRequest) throws IOException {
        Assert.notNull(deleteRequest, "delete request object is null.");
        return restHighLevelClient.delete(deleteRequest, DEFAULT_REQUES_OPTION);
    }

    /**
     * 批量创建索引
     *
     * @param list
     * @return
     * @throws IOException
     */
    public BulkResponse batchCreateIndex(List<IndexRequest> list) throws IOException {
        Assert.notNull(list, "index request list is null.");
        BulkRequest bulkRequest = new BulkRequest();
        for (IndexRequest indexRequest : list) {
            bulkRequest.add(indexRequest);
        }
        return restHighLevelClient.bulk(bulkRequest, DEFAULT_REQUES_OPTION);
    }

    /**
     * 批量更新索引
     *
     * @param list
     * @return
     * @throws IOException
     */
    public BulkResponse batchUpdateIndex(List<UpdateRequest> list) throws IOException {
        Assert.notNull(list, "update request list is null.");
        BulkRequest bulkRequest = new BulkRequest();
        for (UpdateRequest updateRequest : list) {
            bulkRequest.add(updateRequest);
        }
        return restHighLevelClient.bulk(bulkRequest, DEFAULT_REQUES_OPTION);
    }

    /**
     * 批量删除索引
     *
     * @param list
     * @return
     * @throws IOException
     */
    public BulkResponse batchDeleteIndex(List<DeleteRequest> list) throws IOException {
        Assert.notNull(list, "delete request list is null.");
        BulkRequest bulkRequest = new BulkRequest();
        for (DeleteRequest deleteRequest : list) {
            bulkRequest.add(deleteRequest);
        }
        return restHighLevelClient.bulk(bulkRequest, DEFAULT_REQUES_OPTION);
    }


}
